package com.start_eternity.oasissafewalk.service.impl;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.start_eternity.oasissafewalk.mapper.NoticeMapper;
import com.start_eternity.oasissafewalk.pojo.Logg;
import com.start_eternity.oasissafewalk.pojo.Notice;
import com.start_eternity.oasissafewalk.service.LoggService;
import com.start_eternity.oasissafewalk.service.NoticeService;
import com.start_eternity.oasissafewalk.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * @author QT
 * @description 针对表【notice(通知公告表)】的数据库操作Service实现
 * @createDate 2024-01-31 19:06:29
 */
@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private LoggService loggService;

    @Autowired
    private HttpServletRequest request;
    //查询全部
    @Override
    public List<Notice> findAll() {
        List<Notice> notices = noticeMapper.queryAll();
        return notices;
    }

    //分页查询
    @Override
    public List<Notice> queryPage(int current, int size) {
        //IPage -> Page(页码，页容量)
        Page<Notice> page = new Page<>(current,size);
        noticeMapper.selectPage(page, null);
        return page.getRecords();
    }

    //批量插入
    @Override
    public int insertBatching(List<Notice> notices) {
        int i = noticeMapper.insertBatch(notices);
        Logg logg = new Logg();
        logg.setLoggTime(LocalDateTime.now());
        String jwt = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(jwt);
        String name = (String)claims.get("username");
        logg.setLoggName(name);
        logg.setLoggDescription("执行了插入公告的操作,公告内容:" + notices);
        loggService.insert(logg);
        return i;
    }

    //通过id修改
    @Override
    public int updateByNIding(Notice entity) {
        int i = noticeMapper.updateById(entity);
        Logg logg = new Logg();
        logg.setLoggTime(LocalDateTime.now());
        String jwt = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(jwt);
        String name = (String)claims.get("username");
        logg.setLoggName(name);
        logg.setLoggDescription("执行根据ID修改公告,公告内容:" + entity);
        loggService.insert(logg);
        return i;
    }

    //通过集合id批量删除
    public int delBatchId(List<Integer> nId) {
        int i = noticeMapper.deleteBachId(nId);
        Logg logg = new Logg();
        logg.setLoggTime(LocalDateTime.now());
        String jwt = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(jwt);
        String name = (String)claims.get("username");
        logg.setLoggName(name);
        logg.setLoggDescription("执行根据ID删除公告,删除了ID为:" + nId + "的内容");
        loggService.insert(logg);
        return i;
    }

    //通过标题模糊删除
    @Override
    public int delByNTitling(String nTitle) {
        int i = noticeMapper.delByNTitle(nTitle);
        Logg logg = new Logg();
        logg.setLoggTime(LocalDateTime.now());
        String jwt = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(jwt);
        String name = (String)claims.get("username");
        logg.setLoggName(name);
        logg.setLoggDescription("执行根据标题模糊删除公告,删除了nTitle为:" + nTitle + "的相关公告");
        loggService.insert(logg);
        return i;
    }

    //通过id查询
    @Override
    public List<Notice> queryAllByNIding(Integer nId) {
        List<Notice> notices = noticeMapper.queryAllByNId(nId);
        return notices;
    }

    @Override
    public List<Notice> queryByTitle(String nTitle) {
        List<Notice> notices = noticeMapper.queryList(nTitle);
        return notices;
    }
}




